﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Travel.DAL
{
	/// <summary>
	/// 数据访问类:wb_advertisement
	/// </summary>
	public partial class wb_advertisement
	{
		public wb_advertisement()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from wb_advertisement");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Travel.Model.wb_advertisement model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into wb_advertisement(");
			strSql.Append("id,title,adnum,picpath,weburl,cost,height,width,starttime,endTime,type,isuse,comment,js,created_on,created_by,changed_on,changed_by)");
			strSql.Append(" values (");
			strSql.Append("@id,@title,@adnum,@picpath,@weburl,@cost,@height,@width,@starttime,@endTime,@type,@isuse,@comment,@js,@created_on,@created_by,@changed_on,@changed_by)");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50),
					new SqlParameter("@title", SqlDbType.NVarChar,100),
					new SqlParameter("@adnum", SqlDbType.NVarChar,50),
					new SqlParameter("@picpath", SqlDbType.NVarChar,1000),
					new SqlParameter("@weburl", SqlDbType.NVarChar,1000),
					new SqlParameter("@cost", SqlDbType.NVarChar,50),
					new SqlParameter("@height", SqlDbType.Int,4),
					new SqlParameter("@width", SqlDbType.Int,4),
					new SqlParameter("@starttime", SqlDbType.DateTime),
					new SqlParameter("@endTime", SqlDbType.DateTime),
					new SqlParameter("@type", SqlDbType.Int,4),
					new SqlParameter("@isuse", SqlDbType.Int,4),
					new SqlParameter("@comment", SqlDbType.NVarChar,300),
					new SqlParameter("@js", SqlDbType.NVarChar,1000),
					new SqlParameter("@created_on", SqlDbType.DateTime),
					new SqlParameter("@created_by", SqlDbType.NVarChar,50),
					new SqlParameter("@changed_on", SqlDbType.DateTime),
					new SqlParameter("@changed_by", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.id;
			parameters[1].Value = model.title;
			parameters[2].Value = model.adnum;
			parameters[3].Value = model.picpath;
			parameters[4].Value = model.weburl;
			parameters[5].Value = model.cost;
			parameters[6].Value = model.height;
			parameters[7].Value = model.width;
			parameters[8].Value = model.starttime;
			parameters[9].Value = model.endTime;
			parameters[10].Value = model.type;
			parameters[11].Value = model.isuse;
			parameters[12].Value = model.comment;
			parameters[13].Value = model.js;
			parameters[14].Value = model.created_on;
			parameters[15].Value = model.created_by;
			parameters[16].Value = model.changed_on;
			parameters[17].Value = model.changed_by;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Travel.Model.wb_advertisement model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update wb_advertisement set ");
			strSql.Append("title=@title,");
			strSql.Append("adnum=@adnum,");
			strSql.Append("picpath=@picpath,");
			strSql.Append("weburl=@weburl,");
			strSql.Append("cost=@cost,");
			strSql.Append("height=@height,");
			strSql.Append("width=@width,");
			strSql.Append("starttime=@starttime,");
			strSql.Append("endTime=@endTime,");
			strSql.Append("type=@type,");
			strSql.Append("isuse=@isuse,");
			strSql.Append("comment=@comment,");
			strSql.Append("js=@js,");
			strSql.Append("created_on=@created_on,");
			strSql.Append("created_by=@created_by,");
			strSql.Append("changed_on=@changed_on,");
			strSql.Append("changed_by=@changed_by");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@title", SqlDbType.NVarChar,100),
					new SqlParameter("@adnum", SqlDbType.NVarChar,50),
					new SqlParameter("@picpath", SqlDbType.NVarChar,1000),
					new SqlParameter("@weburl", SqlDbType.NVarChar,1000),
					new SqlParameter("@cost", SqlDbType.NVarChar,50),
					new SqlParameter("@height", SqlDbType.Int,4),
					new SqlParameter("@width", SqlDbType.Int,4),
					new SqlParameter("@starttime", SqlDbType.DateTime),
					new SqlParameter("@endTime", SqlDbType.DateTime),
					new SqlParameter("@type", SqlDbType.Int,4),
					new SqlParameter("@isuse", SqlDbType.Int,4),
					new SqlParameter("@comment", SqlDbType.NVarChar,300),
					new SqlParameter("@js", SqlDbType.NVarChar,1000),
					new SqlParameter("@created_on", SqlDbType.DateTime),
					new SqlParameter("@created_by", SqlDbType.NVarChar,50),
					new SqlParameter("@changed_on", SqlDbType.DateTime),
					new SqlParameter("@changed_by", SqlDbType.NVarChar,50),
					new SqlParameter("@id", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.adnum;
			parameters[2].Value = model.picpath;
			parameters[3].Value = model.weburl;
			parameters[4].Value = model.cost;
			parameters[5].Value = model.height;
			parameters[6].Value = model.width;
			parameters[7].Value = model.starttime;
			parameters[8].Value = model.endTime;
			parameters[9].Value = model.type;
			parameters[10].Value = model.isuse;
			parameters[11].Value = model.comment;
			parameters[12].Value = model.js;
			parameters[13].Value = model.created_on;
			parameters[14].Value = model.created_by;
			parameters[15].Value = model.changed_on;
			parameters[16].Value = model.changed_by;
			parameters[17].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wb_advertisement ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wb_advertisement ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Travel.Model.wb_advertisement GetModel(string id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,title,adnum,picpath,weburl,cost,height,width,starttime,endTime,type,isuse,comment,js,created_on,created_by,changed_on,changed_by from wb_advertisement ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)			};
			parameters[0].Value = id;

			Travel.Model.wb_advertisement model=new Travel.Model.wb_advertisement();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Travel.Model.wb_advertisement DataRowToModel(DataRow row)
		{
			Travel.Model.wb_advertisement model=new Travel.Model.wb_advertisement();
			if (row != null)
			{
				if(row["id"]!=null)
				{
					model.id=row["id"].ToString();
				}
				if(row["title"]!=null)
				{
					model.title=row["title"].ToString();
				}
				if(row["adnum"]!=null)
				{
					model.adnum=row["adnum"].ToString();
				}
				if(row["picpath"]!=null)
				{
					model.picpath=row["picpath"].ToString();
				}
				if(row["weburl"]!=null)
				{
					model.weburl=row["weburl"].ToString();
				}
				if(row["cost"]!=null)
				{
					model.cost=row["cost"].ToString();
				}
				if(row["height"]!=null && row["height"].ToString()!="")
				{
					model.height=int.Parse(row["height"].ToString());
				}
				if(row["width"]!=null && row["width"].ToString()!="")
				{
					model.width=int.Parse(row["width"].ToString());
				}
				if(row["starttime"]!=null && row["starttime"].ToString()!="")
				{
					model.starttime=DateTime.Parse(row["starttime"].ToString());
				}
				if(row["endTime"]!=null && row["endTime"].ToString()!="")
				{
					model.endTime=DateTime.Parse(row["endTime"].ToString());
				}
				if(row["type"]!=null && row["type"].ToString()!="")
				{
					model.type=int.Parse(row["type"].ToString());
				}
				if(row["isuse"]!=null && row["isuse"].ToString()!="")
				{
					model.isuse=int.Parse(row["isuse"].ToString());
				}
				if(row["comment"]!=null)
				{
					model.comment=row["comment"].ToString();
				}
				if(row["js"]!=null)
				{
					model.js=row["js"].ToString();
				}
				if(row["created_on"]!=null && row["created_on"].ToString()!="")
				{
					model.created_on=DateTime.Parse(row["created_on"].ToString());
				}
				if(row["created_by"]!=null)
				{
					model.created_by=row["created_by"].ToString();
				}
				if(row["changed_on"]!=null && row["changed_on"].ToString()!="")
				{
					model.changed_on=DateTime.Parse(row["changed_on"].ToString());
				}
				if(row["changed_by"]!=null)
				{
					model.changed_by=row["changed_by"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,adnum,picpath,weburl,cost,height,width,starttime,endTime,type,isuse,comment,js,created_on,created_by,changed_on,changed_by ");
			strSql.Append(" FROM wb_advertisement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,title,adnum,picpath,weburl,cost,height,width,starttime,endTime,type,isuse,comment,js,created_on,created_by,changed_on,changed_by ");
			strSql.Append(" FROM wb_advertisement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM wb_advertisement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from wb_advertisement T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "wb_advertisement";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

